export default class HttpVidReq {
    public static reqvidurl = "";
    constructor(){
        	
    }
    public static async  getList(){
        return this.GET("cameranew/list");
    }
    public static async GET(api: string, params: any = {}, nowUrl: string = "") {
        return new Promise((resolve) => {
            if(!HttpVidReq.reqvidurl)
            HttpVidReq.reqvidurl = window["rstpReq"];
            if (nowUrl == "")
                nowUrl = HttpVidReq.reqvidurl + "/" + api;

            if (params) {
                let paramsArray = [];
                //拼接参数
                Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))

                if (paramsArray.length > 0)
                    nowUrl += ('?' + paramsArray.join('&'));
            }
            //fetch请求
            fetch(nowUrl, {
                method: 'GET',
            })
                .then((response) => response.json())
                .then((json) => {
                    this.dealData(json, resolve, api);
                    // console.log("戴假发"+JSON.stringify(json));
                    // this.setState({ discounts: json.data })
                })
                .catch((error) => {
                    // alert(error)
                    console.error("请求服务器失败：" + api);
                    resolve(null);
                })
        });

    }
    private static dealData(jsonObj: any, resolve: any, api: string) {
        if (jsonObj) {
           resolve(jsonObj);
           return;
              
        }
        console.error('请求失败');
        resolve(null);
        return;
    }

}